package com.weixin.paleituo.fragment;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.weixin.paleituo.R;
import com.weixin.paleituo.activity.BindHouseActivity;
import com.weixin.paleituo.activity.MessageDetailsActivity;
import com.weixin.paleituo.activity.MipcaActivityCapture;
import com.weixin.paleituo.adapter.HomeRecommandAdapter;
import com.weixin.paleituo.adapter.WuyeGrideAdapter;
import com.weixin.paleituo.adapter.WuyeRecycleAdapter;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BaseFragment;
import com.weixin.paleituo.bean.HomeRecommandBean;
import com.weixin.paleituo.bean.MessageBean;
import com.weixin.paleituo.bean.ProductListBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.HeaderAndFooterWrapper;
import com.weixin.paleituo.view.LoadingPagerHead;
import com.weixin.paleituo.view.MyGridView;
import com.weixin.paleituo.view.RecommandDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

/**
 * Author yichao
 * Time  2018/6/11 10:46
 * Dest  物业
 */

public class WuYeFragment extends BaseFragment {
    @BindView(R.id.recycle_wuyefragment)
    RecyclerView mRecycleWuyefragment;
    RecyclerView mRecyclerViewCommod;
    @BindArray(R.array.wuyearray)
    String[] mWuyeName;
    int[] incons = {R.mipmap.wuyefei, R.mipmap.wuyebaoxiu, R.mipmap.xiaoquguanggao, R.mipmap.biaoyang};
    @BindView(R.id.iv_location_wuyefragment)
    ImageView mIvLocationWuyefragment;
    @BindView(R.id.iv_search_wuyefragment)
    ImageView mIvSearchWuyefragment;
    @BindView(R.id.iv_saomiao_wuyefragment)
    ImageView mIvSaomiaoWuyefragment;
    private MyGridView mMyGridView;
    private View mHeadView,mFootView;
    private LoadingPagerHead mLoadingPagerHead;
    private HomeRecommandAdapter mHomeRecommandAdapter;
    private  List<HomeRecommandBean> homeRecommandBeansDataList=new ArrayList<>();
    private RelativeLayout mRlNotice;

    @Override
    public View initView(Context context) {

        mLoadingPagerHead = new LoadingPagerHead(mContext) {
            @Override
            protected void close() {

            }

            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(mContext, R.layout.recycle_wuye_fragment);
            }

            @Override
            protected void reLoading() {
                retry();

            }
        };
        // return UIUtils.inflate(mContext, R.layout.fragment_goods_homefragment);
        // return ;

        //  View contentView = UIUtils.inflate(context, R.layout.recycle_wuye_fragment);


        mHeadView = UIUtils.inflate(context, R.layout.head_view_wuyefragment);
        mMyGridView = mHeadView.findViewById(R.id.mygrideview_head_view_wuyefragment);

        mRlNotice = mHeadView.findViewById(R.id.rl_notice_head_view_wuyefragment);
        mFootView = UIUtils.inflate(mContext, R.layout.layout_goodeshome_footview);

        mRecyclerViewCommod = mFootView.findViewById(R.id.recyclerecommod_fragment_goodshome);

        return mLoadingPagerHead;








    }

    @Override
    protected void initData() {
        super.initData();
        List<ProductListBean> productListBeans = new ArrayList<>();
        for (int i = 0; i < mWuyeName.length; i++) {
            productListBeans.add(new ProductListBean(mWuyeName[i], incons[i]));
        }
        WuyeGrideAdapter wuyeGrideAdapter = new WuyeGrideAdapter(mContext, productListBeans);
        mMyGridView.setAdapter(wuyeGrideAdapter);

        WuyeRecycleAdapter wuyeRecycleAdapter = new WuyeRecycleAdapter(mContext);
        HeaderAndFooterWrapper headerAndFooterWrapper = new HeaderAndFooterWrapper(wuyeRecycleAdapter);
        headerAndFooterWrapper.addHeaderView(mHeadView);
        headerAndFooterWrapper.addFootView(mFootView);


        //推荐
        mHomeRecommandAdapter = new HomeRecommandAdapter(mContext, homeRecommandBeansDataList);
        mRecyclerViewCommod.setItemAnimator(new DefaultItemAnimator());
        mRecyclerViewCommod.setLayoutManager(new GridLayoutManager(mContext, 2));
        mRecyclerViewCommod.addItemDecoration(new RecommandDividerItemDecoration(mContext));
        mRecyclerViewCommod.setAdapter(mHomeRecommandAdapter);

        mRecycleWuyefragment.setLayoutManager(new LinearLayoutManager(mContext));
        mRecycleWuyefragment.setItemAnimator(new DefaultItemAnimator());
        mRecycleWuyefragment.setAdapter(headerAndFooterWrapper);

/**/

        retry();

    }

    /**
     *
     */
    private void retry() {

        Observable<StatusCode<List<HomeRecommandBean>>> homeRecommand = ApiUtils.getApi().getHomeRecommand(BaseApplication.getUserId(), BaseApplication.getToken(), Contans.rows, 1);

        HttpUtil.getInstance().toSubscribe(homeRecommand, new ProgressSubscriber<List<HomeRecommandBean>>(mContext) {
            @Override
            protected void _onNext(StatusCode<List<HomeRecommandBean>> listStatusCode) {
                List<HomeRecommandBean> data = listStatusCode.getData();
                mLoadingPagerHead.showPagerView(Contans.STATE_SUCCEED);
                if (data!=null && data.size()>0){
                    homeRecommandBeansDataList.clear();
                    homeRecommandBeansDataList.addAll(data);
                    mHomeRecommandAdapter.notifyDataSetChanged();
                }


            }

            @Override
            protected void _onError(String message) {
                ToastUtils.makeText(message);
                mLoadingPagerHead.showPagerView(Contans.STATE_ERROR);


            }
        },"",lifecycleSubject,false,true);


    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mMyGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                BindHouseActivity.myStartActvity(mContext);

       /*         switch (position){
                    case 0:
                       // LivingPaymentActivity.myStartActvity(mContext);

                        break;
                    case 1:
                      //  ToastUtils.makeText("物业报修");
                            break;
                    case 2:
                      //  ToastUtils.makeText("小区公告");
                        break;
                    case 3:
                       // ToastUtils.makeText("投诉表扬");
                        break;
                }*/

            }
        });
        mRlNotice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                MessageBean messageBean=new MessageBean();
                String type = "物业头条";
                String content = "热烈庆祝，舒社平台迎来诞生4周年纪念日，为了回馈广大业主及用户，本公司决定在2018年6月29日举行一元贴膜的活动。各种机型，有备而来。全部采用德国优质进口钢化膜，质量保证，耐磨防划。举办地点为金南园活动中心，欢迎各位前往！";
                String newsTime = "2018-06-27 17:16:00";
                String title = "金南园\"舒社一元贴膜\"活动";
                messageBean.setContent(content);
                messageBean.setSummary(title);
                messageBean.setTitle(type);
                messageBean.setNewsTime(newsTime);
                MessageDetailsActivity.startActivity(mContext,messageBean);

            }
        });
    }

    /**
     * 地址
     */
    @OnClick(R.id.rl_locationroot_wuye_fragment)
    public void onMrlLocationWuyefragmentClicked() {

      //  BindHouseActivity.myStartActvity(mContext);

    }

    /**
     * 搜索
     */
    @OnClick(R.id.iv_search_wuyefragment)
    public void onMIvSearchWuyefragmentClicked() {
        //SearchGoodsActivity.startActivity(mContext);
    }

    /**
     * 扫描
     */
    @OnClick(R.id.iv_saomiao_wuyefragment)
    public void onMIvSaomiaoWuyefragmentClicked() {

        Intent intent = new Intent();
        intent.setClass(mContext, MipcaActivityCapture.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      //  mContext.startActivityForResult(intent, activity.SCANNIN_GREQUEST_CODE);
        mContext.startActivity(intent);
        Log.i(TAG,"开启扫描二维码");
    }
}
